package com.own.business.resource.module.minio;

import com.own.business.resource.core.base.AbstractObjectStoreUtil;
import com.own.business.resource.core.model.base.BaseUploadModel;
import com.own.component.common.exception.BusinessSimpleException;
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

/**
 * MinioUtil
 *
 * @author chenxueli
 * @date 2023-07-12 09:12:09
 */
@Slf4j
public class MinioUtil extends AbstractObjectStoreUtil {

    private static boolean IS_CHECK_EXIST = false;

    /**
     * 获取客户端信息
     *
     * @return 客户端
     */
    private MinioClient getClient() {
        var properties = super.getProperties();
        var client = MinioClient.builder()
                .endpoint(properties.getEndPoint())
                .credentials(properties.getKey(), properties.getSecret())
                .build();
        if (!IS_CHECK_EXIST) {
            // 判断是否存在bucket
            try {
                var found = client.bucketExists(BucketExistsArgs.builder().bucket(properties.getBucket()).build());
                if (!found) {
                    client.makeBucket(MakeBucketArgs.builder().bucket(properties.getBucket()).build());
                }
            } catch (Exception e) {
                throw new BusinessSimpleException("bucket not found");
            }
            IS_CHECK_EXIST = true;
        }
        return client;
    }

    /**
     * 上传单个文件
     *
     * @param file 文件信息
     * @return 文件存储对象信息
     */
    @Override
    public BaseUploadModel uploadFile(BaseUploadModel file) {
        var properties = super.getProperties();
        var client = getClient();
        var key = file.getFolder("/") + "/" + file.getFileName();
        var args = PutObjectArgs.builder()
                .bucket(properties.getBucket())
                .object(key)
                .stream(file.getInputStream(), file.getFileSize(), -1)
                .contentType(file.getContentType())
                .build();
        try {
            client.putObject(args);
        } catch (Exception e) {
            log.info(e.getMessage());
            throw new BusinessSimpleException("upload file error");
        }
        if (StringUtils.isBlank(properties.getPosition())) {
            properties.setPosition(properties.getEndPoint() + "/" + properties.getBucket());
        }
        return file;
    }

    /**
     * 验证配置是否正常
     *
     * @return 是否正常
     */
    @Override
    public boolean check() {
        var properties = super.getProperties();
        var name = super.getName();
        var flag = true;
        if (StringUtils.isBlank(properties.getKey())) {
            log.error("资源管理{}:minio key填写为空", name);
            flag = false;
        }
        if (StringUtils.isBlank(properties.getSecret())) {
            log.error("资源管理{}:minio secret填写为空", name);
            flag = false;
        }
        if (StringUtils.isBlank(properties.getBucket())) {
            log.error("资源管理{}:minio bucket填写为空", name);
            flag = false;
        }
        if (StringUtils.isBlank(properties.getEndPoint())) {
            log.error("资源管理{}:minio end-point填写为空", name);
            flag = false;
        }
        return flag;
    }
}
